package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.LabelInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "标签管理")
@RestController
@RequestMapping("/admin/label")
public class LabelController {

    @Autowired
    private LabelInfoService labelInfoService;


    /**
     * 调用查询
     *      1.list -> 查询所有 -> type = null select * from label_info
     *      2. list -> type = 2 -> 查询房间
     *      3. list -> type = 1 -> 查询公寓 select * from label_info where type = 1 or 2;
     *
     *      list?type = 1 type = 2
     *
     * 错误问题:
     *      前端根据类型查询和测试! 400异常
     *      原因: 参数使用枚举接受!!前端传递的是数字 1 2!!类型不匹配
     *           参数使用数字int 前端非得传一个"xxx"
     *      解决: 配置三个数据转化器
     *          springmvc converter [1.定义传话器 2.mvc配置转化器]
     *          jackson @JsonValue[ItemType code]
     *          mybatisplus 或者 mybatis  @EnumValue [ItemType code]
     *          ItemType -> code 属性上
     *          前端 -> json{itemType : 1} ->实体类 {ItemType itemType -> @JsonValue 1 -> itemType枚举中code属性}
     *          Java -> 实体类 (ItemType.room) -> room -> code -> 2 -> 2
     *
     */
    @Operation(summary = "（根据类型）查询标签列表")
    @GetMapping("list")
    public Result<List<LabelInfo>> labelList(@RequestParam(required = false) ItemType type) {
        LambdaQueryWrapper<LabelInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(type != null, LabelInfo::getType, type);
        List<LabelInfo> labelInfos = labelInfoService.list(lambdaQueryWrapper);


        return Result.ok(labelInfos);
    }

    @Operation(summary = "新增或修改标签信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdateLabel(@RequestBody LabelInfo labelInfo) {
        boolean saveOrUpdate = labelInfoService.saveOrUpdate(labelInfo);
        return saveOrUpdate ? Result.ok() : Result.fail();
    }

    @Operation(summary = "根据id删除标签信息")
    @DeleteMapping("deleteById")
    public Result deleteLabelById(@RequestParam Long id) {
        labelInfoService.removeById(id);
        return Result.ok();
    }
}
